import pygame

class Snake:
    def __init__(self):
        self.tela = pygame.display.get_surface()
        self.x = self.tela.get_height() / 2
        self.y = self.tela.get_width() / 2
        self.direcaoh = 0
        self.direcaov = -1
        self.pixels = [(self.y, self.x)]
        self.crash = False
        self.comprimento = 100

    def events(self, event):
        if event.key == pygame.K_UP and self.direcaov != 1:
            self.direcaoh = 0
            self.direcaov = -1
        elif event.key == pygame.K_DOWN and self.direcaov != -1:
            self.direcaoh = 0
            self.direcaov = 1
        elif event.key == pygame.K_LEFT and self.direcaoh != 1:
            self.direcaoh = -1
            self.direcaov = 0
        elif event.key == pygame.K_RIGHT and self.direcaoh != -1:
            self.direcaoh = 1
            self.direcaov = 0

    def move(self):
        self.x += self.direcaoh
        self.y += self.direcaov

        if (self.x, self.y) in self.pixels or self.x <= 0 or self.x >= self.tela.get_width() or self.y <= 0 or self.y >= self.tela.get_height():
            self.crash = True

        self.pixels.insert(0, (self.x, self.y))

        if len(self.pixels) > self.comprimento:
            del self.pixels[self.comprimento]

    def draw(self):
        for x, y in self.pixels:
            self.tela.set_at((x, y), (255, 255, 255))
